﻿
#include <algorithm>
#include <gtest/gtest.h>
#include <iostream>
#include <vector>


#include <learn/alg/alg_equation.h>

using namespace xi;
using namespace xi::alg;

// 测试 NewtonSolver
TEST(TestEquation, solving)
{
    Func11 f1 = [](double x) {
        double y = x + sin(x) - 0.5 * x * x;
        return x;
    };

    Alg_RootOfEquation solver;
    double x0 = solver.solve(f1, 0);
    EXPECT_DOUBLE_EQ(f1(x0), 0);
    Func11 f2 = [](double x) {
        double y = tan(x) + cos(x) - 0.5 * x * x;
        return x;
    };

    x0 = solver.solve(f2, 0);
    EXPECT_DOUBLE_EQ(f2(x0), 0);

    FuncXX f3 = [=](VectorXd x) -> VectorXd { return {f1(x[0]), f2(x[1])}; };
    Vector X0(2);
    solver.solve(f3, X0);
    EXPECT_TRUE(f3(X0) == Vector(2));

    FuncXX f4 = [=](VectorXd x) -> VectorXd { return {f1(f2(x[0])), f2(f1(x[1]))}; };
    Vector X1(2);
    solver.solve(f4, X1);
    EXPECT_TRUE(f4(X1) == Vector(2));
}